/*:
**     face.c - face
*/

#include "a48.h"

/* indices of 3-ring */
#define NEXT3(k)        ((k < 2)? (k+1) : 0)
#define PREV3(k)        ((k > 0)? (k-1) : 2)

using namespace A48;

Face* Face::reuse(Hedge *e0, Hedge *e1, Hedge *e2)
{
  e_ = e0;
  e0->set_next(e1); e1->set_next(e2); e2->set_next(e0);
  e0->set_face(this); e1->set_face(this); e2->set_face(this);
  return this;
}

Hedge* Face::hedge(int k)
{
  switch (k) {
  case 0: return e_;
  case 1: return e_->next();
  case 2: return e_->next()->next();
  }
  throw Error("hedge index");
}

Vertex* Face::vertex(int k)
{
  return hedge(PREV3(k))->org();
}

void  Face::set_hedge(int k, Hedge* h)
{
  Hedge *n = hedge(NEXT3(k));
  Hedge *p = hedge(PREV3(k));
  h->set_next(n);
  p->set_next(h);
  if (k == 0) e_ = h;
}

void Face::link_star_verts()
{
  for (int k = 0; k < 3; k++) 
    vertex(k)->set_star(hedge(NEXT3(k)));
}

void  Face::set_vertex(int k, Vertex* v)
{
  hedge(PREV3(k))->set_org(v);
}

void  Face::set_coordText(int k, Vector3 v)
{
  coordText[k] = v;
}
